package com.sywb.chuangyebao.widget;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.VideoDataDetail;
import com.sywb.chuangyebao.utils.DateTimeUtils;
import com.sywb.chuangyebao.utils.GlideUtils;
import com.sywb.chuangyebao.utils.RegexUtils;
import com.sywb.chuangyebao.utils.Utils;

import org.bining.footstone.adapter.BaseRecyclerAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.adapter.listener.OnItemClickListener;
import org.bining.footstone.utils.SharedUtils;
import org.bining.footstone.utils.TimeUtils;

/**
 * 视频详情页头部
 */
public class VideoDetailHeaderView extends FrameLayout {

    public TextView tvTitle;
    public ImageView ivAvatar;
    public ImageView ivLabel;
    public TextView tvAuthor;
    public TextView tvFans;
    public TextView tvFollow;
    public TextView tvPlayCount;
    public TextView tvPraise;
    public TextView tvIntro;
    public RecyclerView rvRecommend;

    private Activity mActivity;

    private RecommendAdapter recyclerAdapter;
    private VideoDetailHeaderViewListener videoDetailHeaderViewListener;

    public VideoDetailHeaderView(Activity activity) {
        this(activity, null);
    }

    public VideoDetailHeaderView(Activity activity, AttributeSet attrs) {
        this(activity, attrs, 0);
    }

    public VideoDetailHeaderView(Activity activity, AttributeSet attrs, int defStyleAttr) {
        super(activity, attrs, defStyleAttr);

        inflate(activity, R.layout.layout_video_detail_header, this);

        mActivity = activity;

        ivAvatar = findViewById(R.id.iv_avatar);
        ivLabel = findViewById(R.id.iv_label);
        tvAuthor = findViewById(R.id.tv_author);
        tvFans = findViewById(R.id.tv_fans);
        tvFollow = findViewById(R.id.tv_follow);
        tvTitle = findViewById(R.id.tv_title);
        tvPlayCount = findViewById(R.id.tv_play_count);
        tvPraise = findViewById(R.id.tv_praise);
        tvIntro = findViewById(R.id.tv_intro);
        rvRecommend = findViewById(R.id.rv_recommend);
    }

    public void setDetail(VideoDataDetail detail) {

        GlideUtils.getCircleImageToUrl(mActivity, detail.owner.getShowAvatar(), ivAvatar, R.drawable.head_image);
        Utils.setUserRoleIcon(ivLabel, detail.owner.user_role);
        tvAuthor.setText(detail.owner.getShowName());
        tvFans.setText(detail.owner.fans_num + "粉丝");

        ivAvatar.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (videoDetailHeaderViewListener != null) {
                    videoDetailHeaderViewListener.onUserClick();
                }
            }
        });

        tvAuthor.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (videoDetailHeaderViewListener != null) {
                    videoDetailHeaderViewListener.onUserClick();
                }
            }
        });

        boolean isLogin = SharedUtils.getBoolean(Constants.USERISLOGIN, false);
        String openId = SharedUtils.getString(Constants.USEROPENID, "");
        if (isLogin && openId.equals(String.valueOf(detail.owner.uid))) {
            tvFollow.setBackgroundResource(R.drawable.round_line_gray);
            tvFollow.setTextColor(ContextCompat.getColor(mActivity, R.color.colorGray));
            tvFollow.setText("关注");
            tvFollow.setClickable(false);
        } else {
            setIsFollow(detail.owner.is_follow);
        }

        tvTitle.setText(detail.title);
        tvPlayCount.setText(detail.count.getPlayTimesTotal());

        setIsThumb(detail.is_thumb, detail.count.thumb_up);

        tvIntro.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (videoDetailHeaderViewListener != null) {
                    videoDetailHeaderViewListener.onInfroClick();
                }
            }
        });

        tvPraise.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (videoDetailHeaderViewListener != null) {
                    videoDetailHeaderViewListener.onPraiseClick();
                }
            }
        });

        recyclerAdapter = new RecommendAdapter(mActivity);
        rvRecommend.setAdapter(recyclerAdapter);
        rvRecommend.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerAdapter.setDatas(detail.recommend);

        recyclerAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                if (videoDetailHeaderViewListener != null) {
                    videoDetailHeaderViewListener.onRecommenItemClick(recyclerAdapter.getDatas().get(i).video_id);
                }
            }
        });
    }

    public void setIsThumb(boolean isThumb, int thumbCount) {
        Drawable drawable;
        if (isThumb) {
            drawable = getResources().getDrawable(R.drawable.praise_fill);
        } else {
            drawable = getResources().getDrawable(R.drawable.praise);
        }
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tvPraise.setCompoundDrawables(drawable, null, null, null);
        tvPraise.setText(thumbCount > 0 ? RegexUtils.getFormatNum(thumbCount) : "点赞");
    }

    public void setIsFollow(boolean isFollow) {
        if (!isFollow) {
            tvFollow.setBackgroundResource(R.drawable.round_pie_msg_unread);
            tvFollow.setText("+ 关注");
            tvFollow.setClickable(true);
            tvFollow.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (videoDetailHeaderViewListener != null) {
                        videoDetailHeaderViewListener.onFollowClick();
                    }
                }
            });
        } else {
            tvFollow.setBackgroundResource(R.drawable.round_pie_gray);
            tvFollow.setText("已关注");
            tvFollow.setClickable(false);
        }
    }

    public void setVideoDetailHeaderViewListener(VideoDetailHeaderViewListener videoDetailHeaderViewListener) {
        this.videoDetailHeaderViewListener = videoDetailHeaderViewListener;
    }

    public interface VideoDetailHeaderViewListener {

        void onUserClick();

        void onFollowClick();

        void onInfroClick();

        void onPraiseClick();

        void onRecommenItemClick(int videoId);
    }

    class RecommendAdapter extends BaseRecyclerAdapter<VideoDataDetail> {

        private Activity mActivity;

        public RecommendAdapter(Activity activity) {
            super(activity, R.layout.item_video_right_pic);
            this.mActivity = activity;
        }

        @Override
        protected void setItemData(ViewHolderHelper viewHolderHelper, int i, VideoDataDetail data) {
            ImageView ivImg = viewHolderHelper.getView(R.id.iv_img);
            GlideUtils.getNormalIamgeToUrl(mActivity, data.logo, ivImg);
            viewHolderHelper.setText(R.id.tv_title, data.title);
            viewHolderHelper.setText(R.id.tv_duration, DateTimeUtils.secToTime(data.length));
            viewHolderHelper.setVisibility(R.id.tv_tag, false);
            viewHolderHelper.setText(R.id.tv_author, data.owner.getShowName());
            viewHolderHelper.setText(R.id.tv_comment_num, data.count.getPlayTimesTotal());
            viewHolderHelper.setText(R.id.tv_time, TimeUtils.transformDate10(data.publish_time));
        }
    }
}
